package com.leozhang.portalssm.controller;


import com.leozhang.portalssm.entity.Assets;
import com.leozhang.portalssm.entity.Sex;
import com.leozhang.portalssm.service.AssetService;
import com.leozhang.portalssm.util.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

//访问url路径的映射

@RequestMapping("/assets")
@Controller
public class AssetController {


    @Autowired
    private AssetService assetService;
    //代表当访问/assettype/list的时候执行该函数


    @RequiresPermissions("permission:query")
    @RequestMapping("/list")
    public String assetList(){
        //type/sex/list表示当前函数最后返回webapp->WEB-INF->views->type->asset->list.jsp页面
        return "type/asset/list";
    }


    @RequestMapping("/list/page")
    @ResponseBody
    public Result assetListPage(@RequestParam(value = "pno",defaultValue = "1")int pno,
                              @RequestParam(value = "psize",defaultValue ="10")int psize,
                              @RequestParam(value="assetTypeName",defaultValue = "")String assetTypeName,
                              @RequestParam(value="sortField",defaultValue = "")String sortField,
                              @RequestParam(value="sortType",defaultValue = "")String sortType)
    {
        System.out.println("我是测试代码2"+assetService.getListForPage(pno, psize, assetTypeName, sortField, sortType));
        return assetService.getListForPage(pno,psize,assetTypeName,sortField,sortType);
    }


    //跳转至添加页面
    @RequestMapping("/add/page")
    public String toaddpage(){
        return "type/asset/add";
    }

    @RequestMapping("/add")
    public String addAssets(Assets assets){
        assetService.insertAssets(assets);
        return "redirect:/assets/list";
    }


    @RequestMapping("/edit/page")
    public String editPage(Long id, Model model){

        System.out.println("zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"+id);
        Assets assets=assetService.selectAssetsById(id);
        //这里的key命名为formData是为了写别的模块可以快速的改
        model.addAttribute("formData",assets);
        return "type/asset/edit";
    }
    @RequestMapping("/edit")
    public String updateAsset(Assets assets){
        assetService.updateAssets(assets);
        return "redirect:/assets/list";
    }
    @RequestMapping("/delete")
    public String assetsDelete(Long id){

        System.out.println("----------------------------------"+id);
        assetService.deleteAssetById(id);
        return "redirect:/assets/list";
    }



}
